package com.hanlin.tank;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 *
 * @author:hl.yuan
 * @date:2022-01-12
 */
public class TankFrame extends Frame {

    /**
     * 坦克
     */
    Tank myTank = new Tank(100, 500, Dir.UP,Group.GOOD, this);
    /**
     * 子弹
     */
    List<Bullet> bulletList = new ArrayList<Bullet>();
    /**
     * 敌方坦克
     */
    List<Tank> tankList = new ArrayList<>();

    List<Explode> explodes = new ArrayList<>();

    public TankFrame() {
        // 设置窗口大小
        setSize(GlobalVariables.GAME_WIDTH, GlobalVariables.GAME_HEIGHT);
        // 是否可以调整大小
        setResizable(false);
        // 标题
        setTitle("坦克大战");
        // 显示窗口
        setVisible(true);
        this.addKeyListener(new MyAddKeyListener());
        // 监控window的事件，当点击×的时候，关闭窗口
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    /**
     * 窗口在重新出来的时候，触发
     *
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        Color color = g.getColor();
        g.setColor(Color.WHITE);
        g.drawString("子弹的数量：" + bulletList.size(), 10, 60);
        g.drawString("敌方的数量：" + tankList.size(), 10, 80);
        g.setColor(color);
        myTank.paint(g);
        // 画出子弹
        for (int i = 0; i < bulletList.size(); i++) {
            bulletList.get(i).paint(g);
        }
        // 画出敌方坦克
        for (int i = 0; i < tankList.size(); i++) {
            tankList.get(i).paint(g);
        }

        for (int i = 0; i < explodes.size(); i++) {
            explodes.get(i).paint(g);
        }

        // 判断子弹是否和敌方坦克相撞
        for (int i = 0; i < bulletList.size(); i++) {
            for (int j = 0; j < tankList.size(); j++) {
                bulletList.get(i).collideWith(tankList.get(j));
            }
        }

    }

    Image offScreenImage = null;

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GlobalVariables.GAME_WIDTH, GlobalVariables.GAME_HEIGHT);
        }
        Graphics graphics = offScreenImage.getGraphics();
        Color color = graphics.getColor();
        graphics.setColor(Color.black);
        graphics.fillRect(0, 0, GlobalVariables.GAME_WIDTH, GlobalVariables.GAME_HEIGHT);
        graphics.setColor(color);
        paint(graphics);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    /**
     * 自定义内部类
     */
    class MyAddKeyListener extends KeyAdapter {
        // 左右上下按键的状态：false为未按下，true为按下
        boolean tankLeft = false;
        boolean tankUp = false;
        boolean tankRight = false;
        boolean tankDown = false;

        /**
         * 按下时触发
         *
         * @param e
         */
        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_LEFT) {
                tankLeft = true;
            } else if (keyCode == KeyEvent.VK_UP) {
                tankUp = true;
            } else if (keyCode == KeyEvent.VK_RIGHT) {
                tankRight = true;
            } else if (keyCode == KeyEvent.VK_DOWN) {
                tankDown = true;
            } else {
                System.out.println("按下其他键位：" + keyCode);
            }
            setMainTankDir();
        }

        /**
         * 释放时触发
         *
         * @param e
         */
        @Override
        public void keyReleased(KeyEvent e) {
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_LEFT) {
                tankLeft = false;
            } else if (keyCode == KeyEvent.VK_UP) {
                tankUp = false;
            } else if (keyCode == KeyEvent.VK_RIGHT) {
                tankRight = false;
            } else if (keyCode == KeyEvent.VK_DOWN) {
                tankDown = false;
            } else if (keyCode == KeyEvent.VK_D) {
                myTank.fire();
            } else {
                System.out.println("释放了：" + keyCode);
            }
            setMainTankDir();
        }

        // 设置tank的方向
        private void setMainTankDir() {
            // 当没有按下上下左右的按键时，坦克为静止状态。
            if (!tankLeft && !tankUp && !tankRight && !tankDown) {
                myTank.setMoving(false);
            } else {
                myTank.setMoving(true);
                if (tankLeft) {
                    myTank.setDir(Dir.LEFT);
                }
                if (tankUp) {
                    myTank.setDir(Dir.UP);
                }
                if (tankRight) {
                    myTank.setDir(Dir.RIGHT);
                }
                if (tankDown) {
                    myTank.setDir(Dir.DOWN);
                }
            }
        }
    }
}
